package com.ruoyi.comprehensive.service.impl;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.pengjie.SnowflakeIdUtils;
import com.ruoyi.comprehensive.domain.PreventionEnclosure;
import com.ruoyi.comprehensive.domain.VentilationPrevention;
import com.ruoyi.comprehensive.dto.VentilationDto;
import com.ruoyi.comprehensive.dto.VentilationExamineDto;
import com.ruoyi.comprehensive.mapper.PreventionEnclosureMapper;
import com.ruoyi.comprehensive.mapper.VentilationPreventionMapper;
import com.ruoyi.comprehensive.vo.PreventionVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.comprehensive.mapper.VentilationPreventionExamineMapper;
import com.ruoyi.comprehensive.domain.VentilationPreventionExamine;
import com.ruoyi.comprehensive.service.IVentilationPreventionExamineService;

import static com.ruoyi.common.utils.PageUtils.startPage;

/**
 * 通风审核Service业务层处理
 * 
 * @author hcc
 * @date 2023-12-13
 */
@Service
public class VentilationPreventionExamineServiceImpl implements IVentilationPreventionExamineService 
{
    @Autowired
    private VentilationPreventionExamineMapper ventilationPreventionExamineMapper;
    @Autowired
    private VentilationPreventionMapper ventilationPreventionMapper;
    @Autowired
    private PreventionEnclosureMapper preventionEnclosureMapper;

    /**
     * 查询通风审核
     * 
     * @param id 通风审核主键
     * @return 通风审核
     */
    @Override
    public VentilationPreventionExamine selectVentilationPreventionExamineById(String id)
    {
        return ventilationPreventionExamineMapper.selectVentilationPreventionExamineById(id);
    }

    /**
     * 查询通风审核列表
     * 
     * @param ventilationPreventionExamine 通风审核
     * @return 通风审核
     */
    @Override
    public List<VentilationPreventionExamine> selectVentilationPreventionExamineList(VentilationPreventionExamine ventilationPreventionExamine)
    {
        return ventilationPreventionExamineMapper.selectVentilationPreventionExamineList(ventilationPreventionExamine);
    }

    /**
     * 新增通风审核
     * 
     * @param ventilationPreventionExamine 通风审核
     * @return 结果
     */
    @Override
    public int insertVentilationPreventionExamine(VentilationPreventionExamine ventilationPreventionExamine)
    {
        ventilationPreventionExamine.setId(String.valueOf(new SnowflakeIdUtils(5,8).nextId()));
        return ventilationPreventionExamineMapper.insertVentilationPreventionExamine(ventilationPreventionExamine);
    }

    /**
     * 修改通风审核
     * 
     * @param ventilationPreventionExamine 通风审核
     * @return 结果
     */
    @Override
    public int updateVentilationPreventionExamine(VentilationPreventionExamine ventilationPreventionExamine) {
        VentilationPreventionExamine ventilationPreventionExamine1 = new VentilationPreventionExamine();
        BeanUtils.copyProperties(ventilationPreventionExamine,ventilationPreventionExamine1);
        ventilationPreventionExamine1.setUpdateAt(new Date());
        ventilationPreventionExamine1.setUpdateByName(SecurityUtils.getUsername());
        ventilationPreventionExamine1.setUpdateBy(SecurityUtils.getUserId().toString());
        VentilationPrevention ventilationPrevention = ventilationPreventionMapper.selectVentilationPreventionById(ventilationPreventionExamine.getVentilationPreventionId());
        if (ventilationPrevention != null){
            VentilationPrevention ventilationPrevention1 = new VentilationPrevention();
            ventilationPrevention1.setExamineStatus(ventilationPreventionExamine.getExamineStatus());
            ventilationPrevention1.setUpdateAt(new Date());
            ventilationPrevention1.setUpdateByName(SecurityUtils.getUsername());
            ventilationPrevention1.setUpdateBy(SecurityUtils.getUserId().toString());
            ventilationPreventionMapper.updateVentilationPrevention(ventilationPrevention1);
        }
        return ventilationPreventionExamineMapper.updateVentilationPreventionExamine(ventilationPreventionExamine1);
    }

    /**
     * 批量删除通风审核
     * 
     * @param ids 需要删除的通风审核主键
     * @return 结果
     */
    @Override
    public int deleteVentilationPreventionExamineByIds(String[] ids)
    {
        return ventilationPreventionExamineMapper.deleteVentilationPreventionExamineByIds(ids);
    }

    /**
     * 删除通风审核信息
     * 
     * @param id 通风审核主键
     * @return 结果
     */
    @Override
    public int deleteVentilationPreventionExamineById(String id)
    {
        return ventilationPreventionExamineMapper.deleteVentilationPreventionExamineById(id);
    }

    /**
     * 查询通风审核列表
     * @param vo
     * @return
     */
    @Override
    public PageInfo<VentilationExamineDto> getVentilationPreventionExamineList(PreventionVo vo) {
        // 开始分页
        startPage();
        List<VentilationPreventionExamine> ventilationPreventionExamineList = ventilationPreventionExamineMapper.getVentilationPreventionExamineList(vo);
        //  获取分页信息
        PageInfo ventilationExamineDtoInfo = new PageInfo<>(ventilationPreventionExamineList);
        List<VentilationExamineDto> dtoList = ventilationPreventionExamineList.stream().map(ventilationPreventionExamine -> {
            VentilationExamineDto ventilationExamineDto = new VentilationExamineDto();
            BeanUtils.copyProperties(ventilationPreventionExamine, ventilationExamineDto);
            VentilationPrevention ventilationPrevention = ventilationPreventionMapper.selectVentilationPreventionById(ventilationPreventionExamine.getVentilationPreventionId());
            PreventionEnclosure preventionEnclosure = new PreventionEnclosure();
            preventionEnclosure.setPreventionId(ventilationPrevention.getId());
            preventionEnclosure.setEnclosureType("2");
            // 查询附件信息内容
            List<PreventionEnclosure> preventionEnclosures = preventionEnclosureMapper.selectPreventionEnclosureList(preventionEnclosure);
            ventilationExamineDto.setEnclosures(preventionEnclosures);
            ventilationExamineDto.setVentilationPreventionCode(ventilationPrevention.getVentilationPreventionCode());
            ventilationExamineDto.setVentilationPreventionDate(ventilationPrevention.getVentilationPreventionDate());
            ventilationExamineDto.setName(ventilationPrevention.getName());
            return ventilationExamineDto;
        }).collect(Collectors.toList());
        // 将封装后的列表放到分页对象中
        ventilationExamineDtoInfo.setList(dtoList);
        return ventilationExamineDtoInfo;
    }
}
